package com.woniu.deal.order.service.impl;

import cn.hutool.core.util.IdUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.woniu.common.entity.DealOrder;
import com.woniu.common.entity.DealRechargeorder;
import com.woniu.common.entity.DealUseraccount;
import com.woniu.deal.order.feignclient.DealOrderFeign;
import com.woniu.deal.order.mapper.DealOrderMapper;
import com.woniu.deal.order.mapper.DealRechargeorderMapper;
import com.woniu.deal.order.mapper.DealUseraccountMapper;
import com.woniu.deal.order.service.DealOrderPayService;
import com.woniu.deal.order.service.DealOrderService;
import com.woniu.deal.order.service.DealRechargeorderService;
import com.woniu.deal.order.service.DealUseraccountService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.http.HttpServletResponse;
import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.Date;

@Service
public class DealOrderPayServiceImpl implements DealOrderPayService {
    @Autowired
    DealUseraccountService dealUseraccountService;
    @Autowired
    DealOrderMapper dealOrderMapper;
    @Autowired
    DealUseraccountMapper dealUseraccountMapper;
    @Autowired
    DealRechargeorderMapper dealRechargeorderMapper;
    @Autowired
    DealOrderFeign dealOrderFeign;
    @Override
    @Transactional//需要分布式事务控制，研究seata
    public Integer pay(int userId, DealOrder order) {
        DealUseraccount useraccount = dealUseraccountService.getOne(
                new LambdaQueryWrapper<DealUseraccount>().eq(DealUseraccount::getUserId, userId));
        if (useraccount!=null){
            if (useraccount.getUseraccountBalance().compareTo(order.getOrderAmout())>0){
                order.setOrderStatus("待接单");
                order.setOrderPaydate(new SimpleDateFormat("yyyy-MM-dd HH:mm").format(new Date()));
                order.setOrderPaymethod("支付宝");
                dealOrderMapper.updateById(order);
                useraccount.setUseraccountBalance(useraccount.getUseraccountBalance().subtract(order.getOrderAmout()));
                dealUseraccountMapper.updateById(useraccount);
                //调老陈的接口把订单传过去实现自动派单

                return 200;
            }else {
                return 666;
            }
        }
        return 404;
    }

    @Override
    //seata分布式事务管理
    public String rechargeCreateOrder(String token, BigDecimal rechargeamout) {
        //在token中获取user_id,这里暂时用1替代，然后创建充值订单
        DealRechargeorder dealRechargeorder = new DealRechargeorder();
        dealRechargeorder.setRechargeorderId(null);
        dealRechargeorder.setUserId(1);
        dealRechargeorder.setRechargeorderGoodsname("养老平台余额充值");
        dealRechargeorder.setRechargeorderStatus("待支付");
        dealRechargeorder.setRechargeorderTotal(rechargeamout);
        String num = IdUtil.getSnowflake().nextIdStr();
        System.out.println(num);
        dealRechargeorder.setRechargeorderNo(num);
        System.out.println(dealRechargeorder);
        dealRechargeorderMapper.insert(dealRechargeorder);
        //用openfeign调用支付接口进行支付
        String form = dealOrderFeign.pay(dealRechargeorder.getRechargeorderNo()/*,response*/);
        return form;
    }
}
